<?php
/**
 * Created by PhpStorm.
 * User: yyq
 * Date: 2017/3/22
 * Time: 18:00
 */

namespace Order\Controller;


use Common\Controller\AdminbaseController;
use Common\Model\OrderProductModel;

class OrderCommentController extends AdminbaseController
{

    private $order_product_model;

    public function __construct()
    {
        parent::__construct();
        $this->order_product_model = new OrderProductModel();
    }

    public function lists() {
        $fields = [
            'order_sn'  => ["field" => "d.order_sn", "operator" => "like", 'datatype' => 'string'],
            'username' => ["field" => "b.username", "operator" => "like", 'datatype' => 'string'],
            'status'   => ["field" => "a.rep_status", "operator" => "=", 'datatype' => 'string'],
            'if_show'   => ["field" => "a.if_show", "operator" => "=", 'datatype' => 'string'],
        ];

        $where_ands = [];
        if (IS_POST) {
            $_POST['if_show'] = 'yes';
            foreach ($fields as $param => $val) {
                if (isset($_POST[$param]) && !empty($_POST[$param])) {
                    $operator = $val['operator'];
                    $field = $val['field'];
                    $datatype = $val['datatype'];
                    $get = $_POST[$param];
                    $_GET[$param] = $get;

                    if ($operator == "like") {
                        $get = "%$get%";
                    }
                    if ($datatype == 'time')
                        $get = strtotime($get);
                    array_push($where_ands, "$field $operator '$get'");
                }
            }
        } else {
            $_GET['if_show'] = 'yes';
            foreach ($fields as $param => $val) {
                if (isset($_GET[$param]) && !empty($_GET[$param])) {
                    $operator = $val['operator'];
                    $field = $val['field'];
                    $datatype = $val['datatype'];
                    $get = $_GET[$param];
                    if ($operator == "like") {
                        $get = "%$get%";
                    }
                    if ($datatype == 'time')
                        $get = strtotime($get);
                    array_push($where_ands, "$field $operator '$get'");
                }
            }
        }

        $where = join(" and ", $where_ands);
//        $where .= $where ? ' and a.if_show=`'.OrderProductModel::SHOW_YES.'``' : ' a.if_show='.OrderProductModel::SHOW_YES;

        $count = $this->order_product_model
            ->alias('a')
            ->join('LEFT JOIN dr_product as c ON a.pro_id = c.pro_id')
            ->join('LEFT JOIN dr_order as d ON a.order_id = d.id')
            ->join('LEFT JOIN dr_member as b ON d.mid = b.id')
            ->where($where)
            ->count();

        $page = $this->page($count, 20);

        $comments = $this->order_product_model
            ->alias('a')
            ->join('LEFT JOIN dr_product as c ON a.pro_id = c.pro_id')
            ->join('LEFT JOIN dr_order as d ON a.order_id = d.id')
            ->join('LEFT JOIN dr_member as b ON d.mid = b.id')
            ->limit($page->firstRow . ',' . $page->listRows)
            ->field('a.id, a.order_id, a.comment, a.comment_time, a.replay,a.if_show, b.nickname, c.product_name, d.order_sn')
            ->where($where)
            ->order('a.comment_time desc')
            ->select();
//        dump($comments);
        foreach($comments as $k => $v) {
            if($v['if_show'] == OrderProductModel::SHOW_YES) {
                $result[$k]['str_manage'] .= '<a class="js-ajax-dialog-btn"  href="' . U('setShow', ['id' => $v['id'], 'if_show' => 'no']) . '">删除</a>';
            } else {
                $result[$k]['str_manage'] .= '<a class="js-ajax-dialog-btn"  href="' . U('setShow', ['id' => $v['id'], 'if_show' => 'yes']) . '">显示</a>';
            }

            if(!$v['replay']) {
                $result[$k]['str_manage'] .= ' | <a data-toggle="modal" data-target="#myModal"  class="add_ext"  onclick=""   name="' . $v['id'] . '">回复</a>';
            } else {
                $result[$k]['str_manage'] .= ' | 已回复';
            }
            $lists .= '<tr>
                <td>' . ($k + 1) . '</td>
                <td>' . $v['id'] . '</td>
                <td>' . $v['product_name'] . '</td>
                <td>' . $v['order_sn'] . '</td>
                <td>' . $v['nickname'] . '</td>
                <td>' . $v['comment'] . '</td>
                <td>' . $v['comment_time'] . '</td>
                <td>' . ($v['replay'] ? $v['replay'] : '-') . '</td>
                <td>' . $result[$k]['str_manage'] . '</td>
            </tr>';
        }

        $this->assign('lists', $lists);
        $this->assign('formget', I(''));
        $this->display();
    }

    public function delete($id) {
        $rst = $this->product_comment_model->where(['id' => $id])->save(['shows' => ProductCommentModel::SHOW_OFF]);
        IF($rst) {
            $this->success();
        } else {
            $this->error();
        }
    }

    public function setShow($id, $if_show) {
        if($this->order_product_model->where(['id' => $id])->save(['if_show' => $if_show]) == false) {
            $this->error('操作失败');
        } else {
            $this->success('操作成功');
        }
    }

    /**
     * 回复
     */
    public function replay() {
        $id = I('post.id');
        $content = I('post.content');

        if(!$content) {
            $this->error('请填写内容');
        }

        $rst = $this->order_product_model->where(['id' => $id])->save(['replay' => $content]);
        if($rst) {
            $this->success();
        } else {
            $this->error();
        }
    }

}